package com.yswl.pri.home;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.yswl.pri.Constant;
import com.yswl.pri.R;
import com.yswl.pri.browser.MyBrowserActivity;
import com.yswl.pri.common.ToolbarMFragment;
import com.yswl.pri.home.vedio.AnalysisActivity;
import com.yswl.pri.home.vedio.MeijuBrowserActivity;
import com.yswl.pri.home.vedio.SearchActivity;
import com.yswl.pri.guide.adapter.MyAdapter;

import yswl.com.klibrary.base.MActivity;
import yswl.com.klibrary.http.okhttp.MDeviceUtil;

/**
 * Created by kangpAdministrator on 2018/5/8 0008.
 * Emial kangpeng@yunhetong.net
 */

public class MovieChannelFragment extends ToolbarMFragment {
    private static final String TAG = MovieChannelFragment.class.getSimpleName();
    RecyclerView mRecyclerView;
    MyAdapter myAdapter;

    @Override
    public boolean getFragmentMenuSetting() {
        return true;
    }


    protected String getTitle() {
        return "观剧指南";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_movie_channel, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState); 想让子fragment中的menu项中 图片不至于发生错乱，必须使用下面设置的方法
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.search_menu_layout);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                SearchActivity.JumpAct(getMActivity());
                return true;
            }
        });
        toolbar.setTitle(getTitle());
//        getMActivity().setSupportActionBar(toolbar);这句代码是大忌  https://blog.csdn.net/mchaoliu/article/details/51916439
        mRecyclerView = view.findViewById(R.id.guide_rec_view);
        mRecyclerView.requestFocus();
        if (MDeviceUtil.isTvOrMoble(getMActivity())) {
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        } else {
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
        }

        mRecyclerView.setSelected(true);
        myAdapter = new MyAdapter(mContext, R.layout.guide_item, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                Website website = myAdapter.getList().get(position);

                if (website.order < 10) {
                    AnalysisActivity.JumpAct(mContext, website);
                } else if (website.order == 11) {
                    MeijuBrowserActivity.JumpAct(mContext, website.name, website.url);
//                    MActivity.jumpDefaultBrower(mContext, website.url);
                }else if(website.order == 99){
                    MyBrowserActivity.JumpAct(mContext,website.name,website.url);
                }
            }
        });
        myAdapter.setList(Constant.getList());
        mRecyclerView.setAdapter(myAdapter);

    }


}
